<template>
  <div class="table-box">
    <div class="table-search">
      <el-form :inline="true" :model="form">
        <el-form-item label="名称">
          <el-input v-model="form.name" placeholder="名称" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onQuery">查询</el-button>
          <el-button @click="onReset">重置</el-button>
          <el-button type="primary" plain @click="onAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="card content-box">
      <div class="table">
        <el-table :data="tableData" border v-loading="loading">
          <el-table-column prop="id" label="ID" align="center" />
          <el-table-column prop="name" label="名称" align="center" />
          <el-table-column prop="base_coin_id" label="基础币ID" align="center"/>
          <el-table-column prop="base_coin_name" label="基础币名称" align="center"/>
          <el-table prop="quote_coin_id" label="计价币ID" align="center"/>
          <el-table-column prop="quote_coin_name" label="计价币名称" align="center" />
          <el-table-column prop="is_active" label="是否启用" align="center" :formatter="formatter"/>
          <el-table-column label="操作" align="center" width="100" fixed="right">
            <template #default="scope">
              <el-button type="primary" plain @click="onEdit(scope.$index, scope.row)">编辑</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="table-pagination">
          <el-pagination
            :current-page="pageable.pageNum"
            :page-size="pageable.pageSize"
            :page-sizes="[10, 25, 50, 100]"
            :background="true"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageable.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          ></el-pagination>
        </div>
      </div>
    </div>
    <DialogForm
      @cancel="cancel"
      @closeDialog="closeDialog"
      @submitForm="submit"
      :dialogFormVisible="dialogVisible"
      :dialogForm="dialogForm"
      :btnLoading="btnLoading"
    />
  </div>
</template>

<script setup>
import { ref, onMounted, reactive } from "vue";
import { formatter } from "@/utils/utils";
import { swapSymbolApi, swapSymbolEditApi } from "@/api/payServerse/swap";
import DialogForm from "./components/dialogForm.vue";
const tableData = ref([]);
const loading = ref(false);
const btnLoading = ref(false);
const dialogVisible = ref(false);
const formData = {
  name: "",
};
const form = reactive({ ...formData });
const dialogFormData = {
  symbol_id: "",
  name: "",
  base_coin_name: "",
  base_coin_id: "",
  quote_coin_name: "",
  quote_coin_id: "",
  is_active: false,
};
const dialogForm = reactive({ ...dialogFormData });
const pageable = reactive({
  pageNum: 1,
  pageSize: 10,
  total: 0,
});

// 参数
const param = () => {
  return {
    page: pageable.pageNum,
    pagesize: pageable.pageSize,
    name: form.name,
  };
};

// 获取表格数据
const getTbaleData = async () => {
  loading.value = true;

  try {
    const res = await swapSymbolApi(param());
    loading.value = false;
    tableData.value = res.data.results;
    pageable.total = res.data.count;
  } catch (error) {
    loading.value = false;
  }
};
// 重置
const onReset = () => {
  Object.assign(form, formData);
};
// 查询
const onQuery = async () => {
  getTbaleData();
};
//新增
const onAdd = () => {
  dialogVisible.value = true;
};
//关闭弹窗
const closeDialog = () => {
  dialogVisible.value = false;
  Object.assign(dialogForm, dialogFormData);
};

// 取消
const cancel = () => {
  dialogVisible.value = false;
  Object.assign(dialogForm, dialogFormData);
};
// 提交
const submit = async (param) => {
  try {
    btnLoading.value = true;
    const res = await swapSymbolEditApi(param);

    ElMessage({
      message: res.message,
      type: "success",
    });
    dialogVisible.value = false;
    btnLoading.value = false;
    getTbaleData();
  } catch (error) {
    btnLoading.value = false;
    dialogVisible.value = false;
  }
};
// 编辑
const onEdit = (index, row) => {
  Object.assign(dialogForm, {
    symbol_id: row.id,
    name: row.name,
    base_coin_name: row.base_coin_name,
    base_coin_id: row.base_coin_id,
    quote_coin_name: row.quote_coin_name,
    quote_coin_id: row.quote_coin_id,
    is_active: row.is_active == 1 ? true : false,
  });
  dialogVisible.value = true;
};
// 条数
const handleSizeChange = (number) => {
  pageable.pageSize = number;
  getTbaleData();
};
// 页码
const handleCurrentChange = (number) => {
  pageable.pageNum = number;
  getTbaleData();
};
onMounted(() => {
  getTbaleData();
});
</script>
